/**
 * Copyright (C) 2008 Christopher Allen Ogden
 *
 *     This file is part of Odysi.
 *
 *  Odysi is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Odysi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Odysi.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "animationListManager.h"

#include <stdexcept>
#include <fstream>

using std::string;
using std::ifstream;
using std::runtime_error;

void AnimationListManager::load(const std::string &fileName)
{
    ifstream file(fileName.c_str(), std::ios_base::binary);
    if(!file)
        throw runtime_error("Could not open '" + fileName + "'.");

    unsigned int curID = 0;
    while(!file.eof())
    {
        unsigned int id, north, east, south, west, north_walk, east_walk, south_walk, west_walk;

        if(!(file >> id))
        {
            if(file.eof()) return;
            throw runtime_error("Could not read MOB id in '" + fileName + "'.");
        }

        if(id != curID++)
            throw runtime_error("ID does not match in '" + fileName + "'.");

        if(!(file >> north) || !(file >> east) || !(file >> south) || !(file >> west) || !(file >> north_walk) || !(file >> east_walk) || !(file >> south_walk) || !(file >> west_walk))
            throw runtime_error("Could not read tile ID in '" + fileName + "'.");

        AnimationList ai;
        ai.push_back(north);
        ai.push_back(east);
        ai.push_back(south);
        ai.push_back(west);
        ai.push_back(north_walk);
        ai.push_back(east_walk);
        ai.push_back(south_walk);
        ai.push_back(west_walk);

        _mob.push_back(ai);
    }
}
